package com.hmall.gateway.filter;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.function.Consumer;

@Component
public class PrintAnyGatewayFilterFactory extends AbstractGatewayFilterFactory<PrintAnyGatewayFilterFactory.Config> {
    @Override
    public GatewayFilter apply(Config config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                //编写过滤逻辑
                System.out.println("PrintAny filter 执行了");
                return chain.filter(exchange);
            }
        };
    }

    @Data
    public static class Config{
        private String name;
        private String city;
        private String sex;
    }

    //将变量名称依次返回，顺序很重要，将来读取参数时需要按顺序获取
    @Override
    public List<String> shortcutFieldOrder() {
        return List.of("name","city","sex");
    }
//    //将Config字节码传递给父类，父类负责帮我们读取yaml配置
//    public PrintAnyGatewayFilterFactory(){
//        super(Config.class);
//      }


    @Override
    public Class<Config> getConfigClass() {
        return Config.class;
    }
}
